package com.lgy.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class EsConfig {
    public static final String ES_FILE_CATALOGUE = "es/index";
    public static RestHighLevelClient restClient = null;
    @Value("${es.host}")
    public String host;
    @Value("${es.port}")
    public int port;
    @Value("${es.scheme}")
    public String scheme;

    public static RestHighLevelClient getEsInstance() {
        return restClient;
    }

    @PostConstruct
    public void initEs() {
        try {
            // 初始化restClient
            if (restClient != null) {
                restClient.close();
            }
            restClient = new RestHighLevelClient(
                    RestClient.builder(new HttpHost(host, port, scheme))
                            .setRequestConfigCallback(requestConfigBuilder -> {
                                // 该方法接收一个RequestConfig.Builder对象，对该对象进行修改后然后返回。
                                return requestConfigBuilder.setConnectTimeout(5000 * 1000) // 连接超时（默认为1秒）
                                                            .setSocketTimeout(6000 * 1000);// 套接字超时（默认为30秒）//更改客户端的超时限制默认30秒现在改为100*1000分钟
                            }));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*private String getResourceContent(String path) {
        StringBuffer buffer = new StringBuffer();
        if (path != null) {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String s = "";
            try {
                buffer = new StringBuffer();
                while((s = br.readLine()) != null) {
                    buffer.append(s);
                }
            } catch (IOException var6) {
                var6.printStackTrace();
            }
        }
        return buffer.toString();
    }*/
}
